<template>
  <div class="brand-con">
    <!-- 管理卡片 -->
    <el-card class="box-card data-card">
      <div slot="header">
        <span><i class="iconfont icon-dingdan1"></i>商品管理列表</span>
        <el-button
          style="float: right"
          type="primary"
          plain
          size="medium"
          @click="goadd"
          >添加</el-button
        >
      </div>
    </el-card>
    <!-- 品牌管理列表 -->
    <el-table :data="brandList" border style="width: 100%" class="listtable">
      <el-table-column label="" width="90" style="text-align: center">
        <template slot-scope="scope">
          <el-checkbox v-model="scope.row.show_type"></el-checkbox>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="品牌名称"> </el-table-column>
      <el-table-column prop="first_letter" label="品牌首字母">
      </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>

      <el-table-column prop="name" label="商品名称"> </el-table-column>

      <el-table-column label="品牌制造商">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.factory_status"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否显示">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.show_status"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="相关" width="100">
        <template slot-scope="scope">
          <div>商品：{{ scope.row.product_comment_count }}</div>
          <div>评价：{{ scope.row.product_count }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            @click="updateBrand(scope.row.id)"
            type="primary"
            plain
            size="small"
            >编辑</el-button
          >
          <el-button
            @click="delProductBrand(scope.row.id)"
            type="danger"
            size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <!-- size-change 修改每条显示的条数 -->
    <!-- current-change 修改当前页页码 -->
    <!-- current-page 当前的页码 默认 给他一个1 -->
    <!-- page-sizes 当前的每页的数据，能够切换 -->
    <!-- page-size 当前每页有多少条 -->
    <!-- total 数据的总条数 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="page"
    >
    </el-pagination>
  </div>
</template>

<script>
import { fetchBrandPageList, delBrand } from "@/api/product.js";

export default {
  name: "brand",
  data() {
    return {
      brandList: [],
      // 这是传给后台的参数
      listQuery: {
        pageSize: 10, //默认每页显示10条
        pageNum: 1, //默认当前页面是第一页
      },
      total: 0, //总条数
    };
  },
  created() {
    this.getBrandList();
  },
  methods: {
    //获取列表数据
    getBrandList() {
      fetchBrandPageList(this.listQuery).then((res) => {
        console.log(res.data);
        this.brandList = res.data;
        this.total = res.total;
      });
    },
    updateBrand: function (id) {
      this.$router.push("/pms/brandupdate/" +id);
    },
    delProductBrand: function (id) {
      console.log(333,id);
      this.$confirm("此操作将永久删除该条品牌数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delBrand({ id: id }).then((res) => {
            console.log(222, res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
                duration: 2000,
              });
               this.getBrandList();
            }
         
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
    // 修改每一页的数据条数
    handleSizeChange: function (val) {
      console.log("修改页面条数的函数运行了", val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
      this.getBrandList();
    },
    handleCurrentChange: function (val) {
      console.log("修改当前的页码的函数运行了", val);
      this.listQuery.pageNum = val;
      this.getBrandList();
    },
    goadd: function () {
      this.$router.push("/pms/brandAdd");
    },
  },
};
</script>

<style scoped lang="scss">
.brand-con {
  .data-card {
    height: 60px;
  }
  .listtable {
    text-align: center;
    margin-top: 10px;
    margin-bottom: 10px;
    img {
      width: 50px;
    }
  }
  .page {
    float: right;
  }
}
</style>